package com.gsn.chooseGame;

import java.io.File;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenCallback.EventType;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.common.SettingGroup;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.engine.utility.IUtility.IPictureDownloadListener;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.ChatTexture;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;

public class ChooseGameLayer extends GsnLayer implements ClickListener, IPictureDownloadListener {

	private static final String tag = "Choose Game Layer";
	PortalTexture portalTexture;
	GsnImageButton putButton;
	GsnImageButton inviteButton;
	GsnImageButton helpButton;
	GsnImageButton quitButton;
	Label labelName;

	Image avatar;
	private File avatarFile;

	public ChooseGameLayer(float width, float height) {
		super(width, height);
		portalTexture = PortalTexture.inst();
		init();
	}

	public void init() {
		Image bg = new Image(portalTexture.bgPortal);
		bg.width = width;
		bg.height = height;
		addActor(bg);

		Image bgAvatar = new Image(portalTexture.bgAvatar);
		portalTexture.scaleActor(bgAvatar);
		addActor(bgAvatar);
		bgAvatar.y = height * 0.97f - bgAvatar.height;

		Image logo = new Image(portalTexture.logo);
		portalTexture.scaleActor(logo);
		addActor(logo);
		logo.x = (width - logo.width) * 0.5f;
		logo.y = height * 0.97f - logo.height;
		putButton = new GsnImageButton(portalTexture.putButton, portalTexture.putButtonPress);
		portalTexture.scaleActor(putButton);
		float pad = (width - 3 * putButton.getRealWidth()) / 4;
		addActor(putButton);
		putButton.setClickListener(this);
		putButton.x = pad;

		inviteButton = new GsnImageButton(portalTexture.inviteButton, portalTexture.inviteButtonPress);
		portalTexture.scaleActor(inviteButton);
		addActor(inviteButton);
		inviteButton.setClickListener(this);
		inviteButton.x = putButton.x + pad + putButton.getRealWidth();

		helpButton = new GsnImageButton(portalTexture.helpButton, portalTexture.helpButtonPress);
		portalTexture.scaleActor(helpButton);
		addActor(helpButton);
		helpButton.setClickListener(this);
		helpButton.x = inviteButton.x + pad + inviteButton.getRealWidth();

		Image line = new Image(portalTexture.iconLine);
		portalTexture.scaleActor(line);
		addActor(line);
		line.x = width - line.width;
		line.y = height * 0.97f - line.height;

		quitButton = new GsnImageButton(portalTexture.quitButton, portalTexture.quitButtonPress);
		portalTexture.scaleActor(quitButton);
		addActor(quitButton);
		quitButton.setClickListener(this);
		quitButton.x = width - quitButton.getRealWidth() * 1.2f;
		quitButton.y = line.y + (line.height - quitButton.getRealHeight()) / 2;

		labelName = new Label("Cường cận", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelName);
		labelName.x = bgAvatar.x + bgAvatar.width * 0.4f;
		labelName.y = bgAvatar.y + (bgAvatar.height - labelName.height) / 2;

		GalleryGame a = new GalleryGame(width, this);
		FlickScrollPane scrollPane = new FlickScrollPane(a);
		scrollPane.setupOverscroll(30, 50, 100);
		scrollPane.width = width;
		scrollPane.height = a.height;
		addActor(scrollPane);

		scrollPane.y = (height - a.height) / 2;

		avatar = new Image();
		addActor(avatar);

		avatar.width = avatar.height = bgAvatar.height * 0.95f;
		avatar.x = bgAvatar.height * 0.025f;
		avatar.y = bgAvatar.y + bgAvatar.height * 0.025f;
		setInfoUser();
	}

	@Override
	public boolean keyDown(int keycode) {

		if (keycode == Keys.BACK) {
			MyPoker.game.chooseGameScreen.showChangeAcount();
		}
		return super.keyDown(keycode);
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub

		if (MyPoker.game.statePortal != StatePortal.CHOOSE_GAME)
			return;

		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
		PokerSound.playClick();

		if (actor == putButton) {

			((ChooseGameScreen) screen).showCommingDialog();

		} else if (actor == helpButton) {

			((ChooseGameScreen) screen).showHelpDialog();

		} else if (actor == inviteButton) {

			UtilityGettor.inst().inviteFriend();
		} else if (actor == quitButton) {

			MyPoker.game.chooseGameScreen.showChangeAcount();
		}

	}

	public void showParticle(Actor actor, float x, float y) {
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
	}

	@Override
	public void onError(Exception e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFinishLoading(String id, File outFile) {
		// TODO Auto-generated method stub
		avatarFile = outFile;
		Gdx.app.log(tag, "on Finisnh Loading Avatar");
		MyPoker.game.runInGLThread(new TweenCallback() {

			@Override
			public void onEvent(EventType eventType, BaseTween source) {
				try {
					Texture texture = new Texture(new FileHandle(avatarFile));
					TextureRegion region = new TextureRegion(texture);
					avatar.setRegion(region);
					avatarFile = null;
					// Image a = new Image(region);
					// addActor(a);
				} catch (Exception e) {
					avatarFile.delete();
					e.printStackTrace();
				} finally {
					avatarFile = null;
				}

			}
		}, 0);
	}

	public void setInfoUser() {
		UtilityGettor.inst().downloadPictureAsync("me", UserData.avatar, 64, 64, UserData.name + ".png", this);
		labelName.setText(UserData.name);
	}
}
